package com.ybwh.springboot2.common.fastjson.enumat;

import com.alibaba.fastjson.serializer.AfterFilter;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import com.ybwh.springboot2.common.enums.CodeTextEnum;
import com.ybwh.springboot2.common.enums.CodeTextEnumUtils;

import java.io.IOException;
import java.lang.reflect.Type;

public class EnumAtSerializer extends AfterFilter implements ObjectSerializer {

    private static final ThreadLocal<String> fieldNameLocal = new ThreadLocal<>();
    private static final ThreadLocal<Class<? extends CodeTextEnum>> fieldClassLocal = new ThreadLocal<>();


    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {

        System.out.println("writeAfter-------------"+fieldName);

        fieldNameLocal.set((String) fieldName);
        fieldClassLocal.set((Class<? extends CodeTextEnum>) fieldType.getClass());

        serializer.write(object);
    }


    @Override
    public void writeAfter(Object object) {
        System.out.println("writeAfter-------------"+object);

        writeKeyValue(fieldNameLocal.get() + "_label", CodeTextEnumUtils.getDisplayName(fieldClassLocal.get(), object));


    }
}
